﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions

Public Class JSystem

    ''' <summary>
    ''' 驗證是否是EMAIL格式
    ''' </summary>
    ''' <remarks>驗證是否是EMAIL格式</remarks>
    ''' <returns>驗證是否是EMAIL格式</returns>
    Shared Function IsValidEmail(ByVal strIn As String) As Boolean
        ' Return true if strIn is in valid e-mail format.
        Return Regex.IsMatch(strIn, ("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
    End Function

    ''' <summary>
    ''' 驗證是否為身分證字號
    ''' </summary>
    ''' <remarks>驗證是否為身分證字號</remarks>
    ''' <returns>驗證是否為身分證字號</returns>
    Shared Function IsValidIDNO(ByVal strIn As String) As Boolean
        If strIn.Length <> 10 Then
            Return False

        Else

            strIn = StrConv(strIn, VbStrConv.ProperCase)
            Dim prefix As String
            prefix = ""
            Select Case strIn.Chars(0)        '將身份證字號的英文轉換成運算規則的數字
                Case "A"
                    prefix = 10
                Case "B"
                    prefix = 11
                Case "C"
                    prefix = 12
                Case "D"
                    prefix = 13
                Case "E"
                    prefix = 14
                Case "F"
                    prefix = 15
                Case "G"
                    prefix = 16
                Case "H"
                    prefix = 17
                Case "I"
                    prefix = 34
                Case "J"
                    prefix = 18
                Case "K"
                    prefix = 19
                Case "L"
                    prefix = 20
                Case "M"
                    prefix = 21
                Case "N"
                    prefix = 22
                Case "O"
                    prefix = 35
                Case "P"
                    prefix = 23
                Case "Q"
                    prefix = 24
                Case "R"
                    prefix = 25
                Case "S"
                    prefix = 26
                Case "T"
                    prefix = 27
                Case "U"
                    prefix = 28
                Case "V"
                    prefix = 29
                Case "W"
                    prefix = 32
                Case "X"
                    prefix = 30
                Case "Y"
                    prefix = 31
                Case "Z"
                    prefix = 33
            End Select

            Dim N01 As String = prefix.Chars(0)    '抓取英文→數字的十位數部份
            Dim N02 As String = prefix.Chars(1) '個位數部份
            Dim N03 As String = strIn.Chars(1)    '身份證字號數字部份的第一碼
            Dim N04 As String = strIn.Chars(2)
            Dim N05 As String = strIn.Chars(3)
            Dim N06 As String = strIn.Chars(4)
            Dim N07 As String = strIn.Chars(5)
            Dim N08 As String = strIn.Chars(6)
            Dim N09 As String = strIn.Chars(7)
            Dim N10 As String = strIn.Chars(8)    '身份證字號數字部份的倒數第二碼
            Dim N11 As String = strIn.Chars(9)    '身份證字號數字部份的最後一碼，也就是檢查碼

            Dim equation As Integer
            equation = CInt(N01) + CInt(N02) * 9 + CInt(N03) * 8 + CInt(N04) * 7 + CInt(N05) * 6 + CInt(N06) * 5 + CInt(N07) * 4 + CInt(N08) * 3 + CInt(N09) * 2 + CInt(N10)

            If ((10 - equation Mod 10) - N11 = 0) Or ((10 - equation Mod 10) - N11 = 10) Then
                Return True
            Else
                Return False
            End If

        End If

    End Function
    Public Shared Sub insert_mail_threading(ByVal sender_user_pk_id As String, ByVal threading As String, ByVal from_address As String, ByVal sender_address As String, ByVal to_address As String, ByVal bcc_address As String, ByVal cc_address As String, ByVal replyto As String, ByVal isbodyhtml As String, ByVal subject As String, ByVal subjectencoding As String, ByVal body As String, ByVal bodyencoding As String)
        Dim conn As New SqlConnection()
        conn.ConnectionString = "Data Source=XXXXXXX;Initial Catalog=oskay_lab;Persist Security Info=True;User ID=XXXXXXX;Password=XXXXXX"
        Dim rowCount As Integer
        Dim previousConnectionState As ConnectionState
        Dim insertcmd As String
        insertcmd = "INSERT INTO service_mail_Threading(sender_user_pk_id,threading,from_address,sender_address,to_address,bcc_address,cc_address,replyto,isbodyhtml,subject,subjectencoding,body,bodyencoding) VALUES('"
        insertcmd = insertcmd & sender_user_pk_id & "','"
        insertcmd = insertcmd & threading & "','"
        insertcmd = insertcmd & from_address.Replace(",", ".").Replace("..", ".").Trim.Replace(" ", "") & "','"
        insertcmd = insertcmd & sender_address.Replace(",", ".").Replace("..", ".").Trim.Replace(" ", "") & "','"
        insertcmd = insertcmd & to_address.Replace(",", ".").Replace("..", ".").Trim.Replace(" ", "") & "','"
        insertcmd = insertcmd & bcc_address.Replace(",", ".").Replace("..", ".").Trim.Replace(" ", "") & "','"
        insertcmd = insertcmd & cc_address.Replace(",", ".").Replace("..", ".").Trim.Replace(" ", "") & "','"
        insertcmd = insertcmd & replyto & "','"
        insertcmd = insertcmd & isbodyhtml & "','"
        insertcmd = insertcmd & subject & "','"
        insertcmd = insertcmd & subjectencoding & "','"
        insertcmd = insertcmd & body & "','"
        insertcmd = insertcmd & bodyencoding & "')"

        previousConnectionState = conn.State
        Try
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            rowCount = New SqlCommand(insertcmd, conn).ExecuteNonQuery()

        Finally
            If previousConnectionState = ConnectionState.Closed Then
                conn.Close()
            End If
        End Try




    End Sub
    Shared Function CleanStr(ByVal strin As String) As String
        strin = Regex.Replace(strin, "[\u4e00-\u9fa5]", "")
        strin = strin.Replace(";", vbCrLf).Replace(",", vbCrLf).Replace(",", vbCrLf).Replace("：", vbCrLf).Replace("`", vbCrLf).Replace(":", vbCrLf).Replace("'", vbCrLf).Replace(Chr(34), vbCrLf).Replace(" ", vbCrLf).Replace(vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf, vbCrLf).Replace(vbCrLf & vbCrLf & vbCrLf & vbCrLf, vbCrLf).Replace(vbCrLf & vbCrLf & vbCrLf, vbCrLf).Replace(vbCrLf & vbCrLf, vbCrLf).Replace(vbCrLf & vbCrLf, vbCrLf)
        My.Computer.FileSystem.WriteAllText("C:\Test.txt", strin, True)
        Return Regex.Replace(strin, "[\u4e00-\u9fa5]", "")
    End Function
End Class

